<?php

namespace App\Models\Traits;

use App\Models\AdminUsers;
use App\Util\LoginUtil;
use Illuminate\Database\Eloquent\Model;

trait HasUpdatedByAdminIdAttribute
{
    protected static function bootHasUpdatedByAdminIdAttribute()
    {
        static::updating(function (Model $model) {
            if (\Schema::hasColumn($model->getTable(), 'created_by_admin_id')) {
                $updated_by_admin_id = LoginUtil::getAdminId();
                if ($updated_by_admin_id) {
                    $model->setAttribute('updated_by_admin_id', $updated_by_admin_id);
                }
            }
        });
    }

    public function updatedAdmin()
    {
        return $this->belongsTo(AdminUsers::class, 'updated_by_admin_id', 'id');
    }
}
